package edu.neu.swc.cloud.service.impl;

import edu.neu.swc.cloud.dao.EquipmentDao;
import edu.neu.swc.cloud.dao.OrderDao;
import edu.neu.swc.cloud.dao.PlanDao;
import edu.neu.swc.cloud.domain.Equipment;
import edu.neu.swc.cloud.domain.Order;
import edu.neu.swc.cloud.domain.Plan;
import edu.neu.swc.cloud.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("planService")
public class PlanServiceImpl implements PlanService {

    @Autowired
    PlanDao planDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    EquipmentDao equipmentDao;

    @Override
    public List<Plan> findPlanByOrdId(int ord_id) {
        List<Plan> plans = planDao.findPlanByOrdId(ord_id);
        for(Plan plan : plans){
            Equipment equipment = equipmentDao.findEquById(plan.getPlan_equ_id());
            Order order = orderDao.findOrdById(plan.getPlan_ord_id());
            plan.setPlan_equ(equipment);
            plan.setPlan_ord(order);
        }
        return plans;
    }

    @Override
    public void addPlan(Plan plan) {
        planDao.addPlan(plan);
        orderDao.start(plan.getPlan_ord_id());
    }

    @Override
    public int getPlanCounts(int ord_id) {
        return planDao.getPlanCounts(ord_id);
    }

    @Override
    public List<Plan> findPlanByQuery(int ord_id, int pageStart, int pageSize) {
        List<Plan> planList = planDao.findPlanByQuery(ord_id,pageStart,pageSize);
        for(Plan plan : planList){
            Equipment equipment = equipmentDao.findEquById(plan.getPlan_equ_id());
            plan.setPlan_equ_name(equipment.getEq_name());
            plan.setPlan_equ_code(equipment.getEq_code());
            plan.setPlan_equ_cata(equipment.getEq_cata());
        }
        return planList;
    }
}
